//
//  JCWCircleProgerssView.swift
//  CircleProgressView
//
//  Created by wll on 2017/11/9.
//  Copyright © 2017年 wll. All rights reserved.
//  进度圆环视图--代码  马甲包弃用
///为什么不支持autolayout  猜想1:布局应该放在layoutSubviews(待验证)

import UIKit

///背景进度颜色 灰
private let bgLayerColor: CGColor = ColorFromRGBHex_WL(0xcccccc).cgColor
///进度条不渐变时候的颜色 橘
private let progressLayerColor: CGColor = ColorFromRGBHex_WL(0xff7200).cgColor
///渐变色梯度1 浅红
private let shadeColor1: CGColor = ColorFromRGBHex_WL(0xff7200).cgColor
///渐变色梯度2 深红
private let shadeColor2: CGColor = ColorFromRGBHex_WL(0xff0e00).cgColor
///圆环宽度
private let lineWidth: CGFloat = 3.0
///动画时间
private let animationDuration: CFTimeInterval = 1.4


class JCWCircleProgerssView: UIView {
    
    ///设置进度范围[0.0,100.0] 文字内部我会自己拼接% 是否动画
    func wl_setProgress(_ progress: CGFloat, _ animation: Bool, _ status: JCWProductStatus?) {

        self.progressLayer.removeAllAnimations()
        if status == .prepareSale {//预售中 20
            self.gradientLayer.isHidden = true
            self.progressLayer.isHidden = true
            self.progressLayer.strokeEnd = 0
            self.bgLayer.strokeColor = progressLayerColor
        }else if status == .saling{//募集中 30
            self.gradientLayer.isHidden = false
            self.progressLayer.isHidden = false
            self.bgLayer.strokeColor = bgLayerColor
            if animation {
                self.centerLab.dd_setNumber(NSNumber(integerLiteral: Int(progress)), duration: animationDuration, format: "%.0f%%")
                let pathAnima = CABasicAnimation(keyPath: "strokeEnd")
                pathAnima.duration = animationDuration
                pathAnima.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                pathAnima.fromValue = 0
                pathAnima.toValue = progress/100
                pathAnima.fillMode = kCAFillModeForwards
                pathAnima.isRemovedOnCompletion = false
                self.progressLayer.add(pathAnima, forKey: "strokeEndAnimation")
            }else{
                self.centerLab.text = "\(Int(progress))%"
                CATransaction.begin()
                CATransaction.setDisableActions(true)
                CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear))
                CATransaction.setAnimationDuration(animationDuration)
                self.progressLayer.strokeEnd = progress/100
                CATransaction.commit()
            }
        }else{//已售罄
            self.gradientLayer.isHidden = true
            self.progressLayer.isHidden = false
            self.progressLayer.strokeEnd = 0
            self.bgLayer.strokeColor = bgLayerColor
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.createContentView()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createContentView()
    }
    
    func createContentView() {
        self.layer.addSublayer(self.bgLayer)
        self.layer.addSublayer(self.progressLayer)
        self.layer.addSublayer(self.gradientLayer)
        self.gradientLayer.mask = self.progressLayer
        self.addSubview(self.centerLab)
        _ = self.centerLab.sd_layout().centerXEqualToView(self)?.centerYEqualToView(self)?.autoHeightRatio(0)
        self.centerLab.setSingleLineAutoResizeWithMaxWidth(self.bounds.size.width)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.createContentView()
//        fatalError("init(coder:) has not been implemented")
    }
    
    ///背景圆环
    lazy var bgLayer: CAShapeLayer = {
        let temp = CAShapeLayer()
        temp.strokeColor = bgLayerColor//圆环色
        temp.fillColor = UIColor.clear.cgColor//填充色
        temp.lineCap = kCALineCapButt//这个参数主要是调整环型进度条边上是不圆角，主要有三个参数kCALineCapRound(圆角)，kCALineCapButt（直角），kCALineCapSquare（这个参数设了跟直角一样）
        temp.lineWidth = lineWidth//宽度
        temp.path = UIBezierPath(roundedRect: CGRect(x: lineWidth/2, y: lineWidth/2, width: self.bounds.size.width-lineWidth, height: self.bounds.size.height-lineWidth), cornerRadius: self.bounds.size.width/2.0).cgPath//圆形路径
        return temp
    }()
    
    
    ///进度圆环
    lazy var progressLayer: CAShapeLayer = {
        let temp = CAShapeLayer()
        temp.fillColor = UIColor.clear.cgColor
        temp.strokeColor = progressLayerColor
        temp.lineWidth = lineWidth
        temp.lineCap = self.bgLayer.lineCap
        temp.path = self.bgLayer.path
        temp.strokeStart = 0//开始位置 默认平面坐标系(0,1)
        temp.strokeEnd = 0
        return temp
    }()
    
    ///渐变图层 左右分割
    lazy var gradientLayer: CAGradientLayer = {
        let temp = CAGradientLayer()
        temp.frame = self.bounds
        temp.colors = [shadeColor1,shadeColor2]
        temp.locations = [0,0.5]
        temp.startPoint = CGPoint(x: 0.6, y: 0)
        temp.endPoint = CGPoint(x: 0, y: 0)
        return temp
    }()
    
    ///进度数字标签
    lazy var centerLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = ColorFromRGBHex_WL(0xfc291c)
        temp.font = Font_WL(11)
        temp.text = "0%"
        return temp
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
}

